import Vue from 'vue'
import Router from 'vue-router'
import frontIndex from '../frontViews/frontIndex.vue'
import login from "../frontViews/login.vue"
import register from "../frontViews/register.vue"
import backendIndex from "../backend_views/backendIndex.vue"
import shaoEr from "../backend_views/course/kids.vue"
import chengRen from "../backend_views/course/adult.vue"
import match from "../backend_views/other/match.vue"
import Index from "../backend_views/other/index.vue";
import welcome from "../frontViews/others/welcome.vue";
import SaiShi from "../frontViews/others/saiShi.vue";
import AdultCourse from "../frontViews/course/adult-course.vue";
import KidsCourse from "../frontViews/course/kids-course.vue";
import userManage from "../backend_views/other/userManage.vue";

Vue.use(Router)

const router =  new Router({
  // mode: 'history', //用于去除路径中的#号
  routes: [
    // {
    //   path: '/',
    //   name: 'HelloWorld',
    //   component: HelloWorld
    // },
    //前台路由
    {
      path: '/',
      name: 'frontIndex',
      component: frontIndex,
      redirect: '/welcome',
      children:[
        {
          path: '/welcome',
          name: 'welcome',
          component: welcome
        },
        {
          path: '/shiPin',
          name: 'shiPin',
          component: () => import('../frontViews/video/shiPin.vue'),
        },
            // {
            //   path: 'popping',
            //   name: 'popping-video',
            //   component: PoppingVideo
            // },
            // {
            //   path: 'breaking',
            //   name: 'breaking-video',
            //   component: BreakingVideo
            // },
        {
          path:'/shiPin/player',
          meta: {
            title: 'player'
          },
          component: ()=> import('../frontViews/video/videoPlay.vue'),
        },
        {
          path: '/course',
          name: 'course',
          component: () => import('../frontViews/course/course.vue'),
          children: [
            {
              path: 'adult',
              name: 'adult-course',
              component: AdultCourse
            },
            {
              path: 'kids',
              name: 'kids-course',
              component: KidsCourse
            },
          ]
        },
        {
          path: '/saiShi',
          name: 'saiShi',
          component: SaiShi
        },
        {
          path: '/studio',
          name: 'studio',
          component: ()=> import('../frontViews/others/studio.vue')
        },
        {
          path: '/studio/homepage',
          name: 'studio-home-page',
          component: ()=> import('../frontViews/others/studio-homepage.vue')
        },
      ]
    },
    //登录页路由
    {
      path: '/login',
      name: 'login',
      component: login
    },
    //注册页路由
    {
      path: '/register',
      name: 'register',
      component: register
    },
    //后台路由
    {
      path:'/backendLogin',
      meta: {
        title:'后台登录页'
      },
      name:'backendLogin',
      component:() => import('../backend_views/backendLogin.vue'),
    },
    {
      path: '/backendIndex',
      meta: {
        title:'主页'
      },
      name: 'backendIndex',
      component: backendIndex,
      redirect: '/index',
      children: [
        {
          path: '/index',
          name: 'index',
          meta: {
            title:'首页'
          },
          component: Index
        },
        {
          path:'video',
          meta: {
            title:'视频'
          },
          component: () => import('../backend_views/video/video.vue'),
          children: [
              {
              path:'/video/videoIndex',
              meta: {
                title: 'videoIndex'
              },
              component: ()=>import('../backend_views/video/videoIndex.vue')
            },
            //   {
            //   path:'/video/breaking',
            //   meta: {
            //     title: 'breaking'
            //   },
            //   component: brkIndex,
            // },
          ]
        },
        {
          path:'course',
          meta: {
            title:'课程'
          },
          component: () => import('../backend_views/course/course.vue'),
          children: [
              {
              path:'/course/chengRen',
              meta: {
                title: '成人班'
              },
              component: chengRen,
            },
                {
              path:'/course/shaoEr',
              meta: {
                title: '少儿班'
              },
              component: shaoEr,
            },
          ]
        },
          {
        path:'/other/match',
        meta: {
          title: '赛事'
        },
        component: match,
      },
        {
          path: '/backendStudio',
          meta:{
            title:'工作室'
          },
          component: () => import('../backend_views/studio/backendStudio.vue'),
          children: [
                  {
              path:'/backendStudio/studioInfo',
              meta: {
                title: '工作室信息'
              },
              component: ()=>import('../backend_views/studio/studioInfo.vue'),
            },
            {
              path:'/backendStudio/studioMember',
              meta: {
                title: '工作室人员'
              },
              component: ()=>import('../backend_views/studio/studioMember.vue'),
            },
          ]
        },

        {
        path:'/other/userManage',
        meta: {
          title: '用户管理'
        },
        component: userManage,
        },
      ]
    },
  ]
})

export default router;
// 挂载路由守卫，to表示要访问的地址，from表示从哪里来，next是下一个要做的动作
router.beforeEach((to,from,next)=>{
  if (to.path ==='/backendLogin') {
    return next()
  }
  if (to.path === '/welcome') {
    return next()
  }
  if (to.path === '/shiPin') {
    return next()
  }
  if (to.path === '/course') {
    return next()
  }
  if (to.path === '/course/adult') {
    return next()
  }
  if (to.path === '/course/kids') {
    return next()
  }
  if (to.path === '/saiShi') {
    return next()
  }
  if (to.path === '/studio') {
    return next()
  }
  if (to.path === '/studio/homepage') {
    return next()
  }
  if (to.path === '/login') {
    return next()
  }
  if (to.path === '/register') {
    return next()
  }
  if (to.path === '/#/login') {
    return next()
  }
  if (to.path === '/#/register') {
    return next()
  }
  if (to.path === '/shiPin/player') {
    return next()
  }
  const token = sessionStorage.getItem('token')
  if (!token) {
    return next('/backendLogin')
  }else {
    next()
  }
  return next()
})
